/**
 * 全局插件配置资源（vue插件部分）
 * @module utils/plugin
 */
import router from '@/router'
import * as ElIcons from '@element-plus/icons-vue'
import { selectDictLabel } from './util'

export default {
	install: function (App, options) {
		for (const name in ElIcons) {
			/*
			 * 以elIcon为前缀的命名规则，是为了适配已经使用属性名添加图标的内置组件，
			 * 例如：<el-button icon="el-icon-refresh"></el-button>
			 */
			App.component(name, ElIcons[name])
		}
		/**
		 * 原型链全局挂载方法
		 */
		window['$router'] = App.config.globalProperties.$router = router
		window['$store'] = App.config.globalProperties.$store = {
			com: {},
			dict: {},
			guarder: {},
			user: {}
		}

		window['selectDictLabel'] = App.config.globalProperties.selectDictLabel = selectDictLabel
		window['resetForm'] = App.config.globalProperties.resetForm = function (refName) {
			if (refName) {
				// 不是公用的my-form标签的ref逻辑
				refName?.resetFields?.() || refName?.$refs?.refMyForm?.resetFields?.()
			}
		}

		/**
		 * vite动态加载图片静态文件
		 * @param {string} path 文件路径
		 * @returns
		 */
		const modules = import.meta.glob('../assets/img/**/*', { eager: true })
		App.config.globalProperties.getImgUrl = (path) => {
			if (path && modules) {
				if (path.includes('../assets/img/')) {
					return modules[path]?.default
				} else {
					return modules[`../assets/img/${path}`]?.default
				}
			}
		}
	}
}
